"""
Case Type   : 兼容M*数据库subdate函数
Case Name   : SUBDATE(time, interval)函数，参数1为time类型时，参数2中的时间单位测试
Create At   : 2023.01.29
Owner       : z@wan005
Description :
    1、interval的时间单位为year
    2、interval的时间单位为month
    3、interval的时间单位为day
    4、interval的时间单位为hour
    5、interval的时间单位为minute
    6、interval的时间单位为second
    7、interval的时间单位为day to hour
    8、interval的时间单位为day to minute
    9、interval的时间单位为day to second
    10、interval的时间单位为hour to minute
    11、interval的时间单位为hour to second
    12、interval的时间单位为minute to second
    13、清理环境
Expect      :
    1、报错
    2、报错
    3、成功
    4、成功
    5、成功
    6、成功
    7、成功
    8、成功
    9、成功
    10、成功
    11、成功
    12、成功
    13、成功
History     :modified by z@wan005，2023.09.26，select场景不报错，修改断言
             modified by zhanghuan，2024.8.2，适配最新代码，修改断言
             modified by zhanghuan96，2024.8.26，IAHP3M问题修复，代码变更
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest07(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_subdate_0007"

    def test_subdate(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:interval的时间单位为year;expect:弹出warning并返回null--'
        self.log.info(text)
        sql_cmd = "select subdate(time'23:59:59', interval 1 year);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn('subdate \n---------\n \n(1 row)', sql_res, '查询失败')

        text = '--step2:interval的时间单位为month;expect:弹出warning并返回null--'
        self.log.info(text)
        sql_cmd = "select subdate(time'23:59:59', interval 1 month);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn('WARNING:  time field value out of range', sql_res,
                      '执行失败' + text)
        self.assertIn('subdate \n---------\n \n(1 row)', sql_res,
                      '查询失败')

        text = '--step3:interval的时间单位为day;expect:成功--'
        self.log.info(text)
        sql_cmd = "select subdate(time'23:59:59', interval 1 day);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual('-00:00:01', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)

        text = '--step4:interval的时间单位为hour;expect:成功--'
        self.log.info(text)
        sql_cmd = "select subdate(time'23:59:59', interval 1 hour);" \
                  "select subdate(time'23:59:59', interval 1.999 hour);" \
                  "select subdate(time'23:59:59', interval 1.444444 hour);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 14, '行数不到14行')
        self.assertEqual('22:59:59', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('21:59:59', sql_res.splitlines()[7].strip(),
                         '执行失败' + text)
        self.assertEqual('22:59:59', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step5:interval的时间单位为minute;expect:成功--'
        self.log.info(text)
        sql_cmd = "select subdate(time'23:59:59', interval 59 minute);" \
                  "select subdate(time'23:59:59', interval 59.9 minute);" \
                  "select subdate(time'23:59:59', interval 59.444 minute);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 14, '行数不到14行')
        self.assertEqual('23:00:59', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('22:59:59', sql_res.splitlines()[7].strip(),
                         '执行失败' + text)
        self.assertEqual('23:00:59', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step6:interval的时间单位为second;expect:成功--'
        self.log.info(text)
        sql_cmd = "select subdate(time'83:59:59', interval 59 second);" \
                  "select subdate(time'83:59:59', interval 59.999 second);" \
                  "select subdate(time'83:59:59', interval 59.4445 second);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 14, '行数不到14行')
        self.assertEqual('83:59:00', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('83:58:59.001', sql_res.splitlines()[7].strip(),
                         '执行失败' + text)
        self.assertEqual('83:58:59.5555', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step7:interval的时间单位为day to hour;' \
               'expect:成功(小数点不会作为复合单位的分隔符，小数位均舍弃)--'
        self.log.info(text)
        sql_cmd = "set dolphin.support_interval_to =on;" \
                  "select subdate(time'823:59:59', interval 9 day to hour);" \
                  "select subdate" \
                  "(time'823:59:59.99999', interval 9.999 day to hour);" \
                  "select subdate" \
                  "(time'823:59:59.9', interval 9.4445 day to hour);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 14, '行数不到14行')
        self.assertIn('814:59:59', sql_res, '执行失败' + text)
        self.assertIn('-391:00:00.00001', sql_res, '执行失败' + text)
        self.assertIn('', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step8:interval的时间单位为day to minute;expect:成功--'
        self.log.info(text)
        sql_cmd = "set dolphin.support_interval_to =on;" \
                  "select subdate(time'23:59:59', interval 9 day to minute);" \
                  "select subdate" \
                  "(time'23:59:59', interval 9.999 day to minute);" \
                  "select subdate" \
                  "(time'23:59:59', interval 9.4445 day to minute);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual('23:50:59', sql_res.splitlines()[3].strip(),
                         '执行失败' + text)
        self.assertEqual('-01:39:01', sql_res.splitlines()[8].strip(),
                         '执行失败' + text)
        self.assertEqual('-59:05:01', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step9:interval的时间单位为day to second;expect:成功--'
        self.log.info(text)
        sql_cmd = "set dolphin.support_interval_to =on;" \
                   "select subdate" \
                  "(time'23:59:59', interval 59 day to second);" \
                  "select subdate" \
                  "(time'23:59:59', interval 59.999 day to second);" \
                  "select subdate" \
                  "(time'23:59:59', interval 59.4445 day to second);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 14, '行数不到14行')
        self.assertEqual('23:59:00', sql_res.splitlines()[3].strip(),
                         '执行失败' + text)
        self.assertEqual('22:44:20', sql_res.splitlines()[8].strip(),
                         '执行失败' + text)
        self.assertEqual('21:46:54', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step10:interval的时间单位为hour to minute;expect:成功--'
        self.log.info(text)
        sql_cmd = "set dolphin.support_interval_to =on;" \
                   "select subdate" \
                  "(time'23:59:59', interval 59 hour to minute);" \
                  "select subdate" \
                  "(time'23:59:59', interval 59.999 hour to minute);" \
                  "select subdate" \
                  "(time'23:59:59', interval 59.4445 hour to minute);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual('23:00:59', sql_res.splitlines()[3].strip(),
                         '执行失败' + text)
        self.assertEqual('-51:39:01', sql_res.splitlines()[8].strip(),
                         '执行失败' + text)
        self.assertEqual('-109:05:01', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step11:interval的时间单位为hour to second;expect:成功--'
        self.log.info(text)
        sql_cmd = "set dolphin.support_interval_to =on;" \
                   "select subdate" \
                  "(time'23:59:59', interval 59 hour to second);" \
                  "select subdate" \
                  "(time'23:59:59', interval 59.999 hour to second);" \
                  "select " \
                  "subdate(time'23:59:59', interval 59.4445 hour to second);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 14, '行数不到14行')
        self.assertEqual('23:59:00', sql_res.splitlines()[3].strip(),
                         '执行失败' + text)
        self.assertEqual('22:44:20', sql_res.splitlines()[8].strip(),
                         '执行失败' + text)
        self.assertEqual('21:46:54', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '--step12:interval的时间单位为minute to second;expect:成功--'
        self.log.info(text)
        sql_cmd = "set dolphin.support_interval_to =on;" \
                   "select subdate" \
                  "(time'23:59:59', interval 59 minute to second);" \
                  "select subdate" \
                  "(time'23:59:59', interval 59.999 minute to second);" \
                  "select subdate" \
                  "(time'23:59:59', interval 59.4445 minute to second);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 14, '行数不到14行')
        self.assertEqual('23:59:00', sql_res.splitlines()[3].strip(),
                         '执行失败' + text)
        self.assertEqual('22:44:20', sql_res.splitlines()[8].strip(),
                         '执行失败' + text)
        self.assertEqual('21:46:54', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

    def tearDown(self):
        text = '----step13:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
